﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RAP.Framework.Libary.Utils.Collections.Dictionary
{
    /// <summary>
    /// 范型的有序键值对象
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public class ListDictionary<TKey, TValue> : ListDictionary, IDictionary<TKey, TValue>
    {
        public TValue this[ TKey key ]
        {
            get
            {
                return ( TValue )base[ key ];
            }

            set
            {
                base[ key ] = value;
            }
        }

        ICollection<TKey> IDictionary<TKey, TValue>.Keys
        {
            get
            {
                return base.Keys.Cast<TKey>( ).ToArray( );
            }
        }

        ICollection<TValue> IDictionary<TKey, TValue>.Values
        {
            get
            {
                return base.Values.Cast<TValue>( ).ToArray( );
            }
        }

        public void Add( KeyValuePair<TKey, TValue> item )
        {
            base.Add( item.Key, item.Value );
        }

        public void Add( TKey key, TValue value )
        {
            base.Add( key, value );
        }

        public bool Contains( KeyValuePair<TKey, TValue> item )
        {
            return base.Contains( item.Key );
        }

        public bool ContainsKey( TKey key )
        {
            return base.Contains( key );
        }

        public void CopyTo( KeyValuePair<TKey, TValue>[ ] array, int arrayIndex )
        {
            var keys = this.getKeys( );
            for ( int i = arrayIndex; i < keys.Count; i++ )
            {
                var key = keys[ i ];
                var value = this[ keys[ i ] ];
                array[ i - arrayIndex ] = new KeyValuePair<TKey, TValue>( ( TKey )key, ( TValue )value );
            }
        }

        public bool Remove( KeyValuePair<TKey, TValue> item )
        {
            return this.Remove( item.Key );
        }

        public bool Remove( TKey key )
        {
            if ( base.Contains( key ) )
            {
                base.Remove( key );
                return true;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue( TKey key, out TValue value )
        {
            value = default( TValue );
            if ( this.Contains( key ) )
            {
                value = this[ key ];
                return true;
            }
            return false;
        }

        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator( )
        {
            foreach ( DictionaryEntry ety in this )
            {
                yield return new KeyValuePair<TKey, TValue>( ( TKey )ety.Key, ( TValue )ety.Value );
            }

        }
    }
}
